package com.carrental.api.ums.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.carrental.common.response.CommonPage;
import com.carrental.common.response.CommonResult;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.ums.domain.bo.UmsUserBo;
import com.carrental.api.ums.domain.vo.UmsUserVo;
import com.carrental.api.ums.service.IUmsUserService;
import com.carrental.common.annotation.Log;
import com.carrental.common.enums.BusinessType;
import com.carrental.framework.config.SaTokenConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员信息Controller
 *
 * @author liangyi
 * @date 2022-03-20
 */
@Api(tags = "会员信息管理")
@RestController
@RequestMapping("/ums/user")
public class UmsUserController {
    @Autowired
    private IUmsUserService userService;
    @Autowired
    private SaTokenConfig tokenConfig;

    /**
     * 导出会员信息列表
     */
    @ApiOperation("导出会员信息列表")
    @SaCheckPermission("ums:user:export")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated UmsUserBo bo, HttpServletResponse response) {
        //List<UmsUserVo> list = userService.queryList(bo);
        //ExcelUtil.exportExcel(list, "会员信息", UmsUserVo.class, response);
    }

    @PostMapping()
    @ApiOperation("添加用户")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> userCreate(@RequestBody UmsUserBo user) {
        return CommonResult.success(userService.create(user));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> userRemove(@ApiParam(name = "id", value = "用户标识", required = true)
                                           @PathVariable Long id) {
        return CommonResult.success(userService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改用户信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> userUpdate(
        @ApiParam(name = "id", value = "用户标识", required = true)
        @PathVariable Long id, @RequestBody UmsUserBo user) {
        return CommonResult.success(userService.update(id, user));
    }

    @GetMapping()
    @ApiOperation("获取用户列表")
    public CommonResult<List<UmsUserVo>> userList(CommonQueryParam commonQueryParam) {
        return CommonResult.success(userService.userList(commonQueryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户详情")
    public CommonResult<UmsUserVo> userObj(@PathVariable Long id) {
        return CommonResult.success(userService.detail(id));
    }

    @GetMapping("/me")
    @ApiOperation("获取当前用户信息")
    public CommonResult<UmsUserVo> getMyInfo() {
        return CommonResult.success(userService.getMyInfo());
    }

    @GetMapping("/page")
    @ApiOperation("获取用户分页")
    public CommonResult<CommonPage<UmsUserVo>> userPage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(userService.userPage(commonQueryParam)));
    }
}
